package com.vote.controller;

import com.alibaba.fastjson.JSONObject;
import com.vote.entity.AuthUser;
import com.vote.service.AuthUserService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

/**
 * Created by PC-liuwei on 2017/5/8.
 */
@Controller
public class LoginController {
    Logger logger = LoggerFactory.getLogger(LoginController.class);

    @Autowired
    private AuthUserService authUserService;

    @RequestMapping("/login")
    public String login(){
        return "html/login";
    }

    @ResponseBody
    @RequestMapping("/doLogin")
    public String doLogin(HttpServletRequest request){
        String userName = request.getParameter("user.userName");
        String pwd = request.getParameter("user.passWord");
        AuthUser user =  authUserService.getAuthUser(userName,pwd);
        JSONObject json = new JSONObject();
        if(user != null){
            if(user.getStatus()){
                HttpSession session = request.getSession();
                session.setAttribute("userId",user.getId());
                session.setAttribute("userName",user.getUserName());
                json.put("status","1");
            }else{
                json.put("status","2");
            }
        }else{
            json.put("status","3");
        }
        return json.toString();
    }

    @RequestMapping("/index")
    public String index(HttpServletRequest request){
        String userId = request.getSession().getAttribute("userId").toString();
        String userName = request.getSession().getAttribute("userName").toString();
        logger.info("userId:"+userId+",userName:"+userName);
        return "html/index";
    }

    @RequestMapping("/welcome")
    public String welcome(){
        return "html/welcome";
    }
}
